//
//  ChangeInfoViewController.m
//  CommunityApp
//
//  Created by 秦乾龙 on 16/12/14.
//  Copyright © 2016年 秦乾龙. All rights reserved.
//

#import "ChangeInfoViewController.h"
#import "AFNetworking.h"//主要用于网络请求方法
#import "UIKit+AFNetworking.h"//里面有异步加载图片的方法
#import "AFHTTPSessionManager.h"
#import "UIButton+WebCache.h"
#import "UIImageView+WebCache.h"
#import "NavView.h"
#import "User.h"
#import "ZYTableViewCell.h"
#import "ChangeNameViewController.h"
#import "ChangeSexViewController.h"
#import "ChangePersonSignViewController.h"
#import "ChangeLoveSignViewController.h"
#import "ChangeAddressViewController.h"
#import "AddressListViewController.h"
#define kScreenW [UIScreen mainScreen].bounds.size.width
#define kScreenH [UIScreen mainScreen].bounds.size.height
#define kScale   ([UIScreen mainScreen].bounds.size.width)/750

@interface ChangeInfoViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,retain) UITableView *mainTableView;
@property (nonatomic,retain) NavView *nav;
@property (nonatomic,retain) NSArray *titleImageNameArr;
@property (nonatomic,retain) NSArray *titleArr;
@property(nonatomic,retain)User *user;

@end

@implementation ChangeInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.titleView = [ViewFactory makeNavigationTitle:@"修改资料"];
    
    UIBarButtonItem *backBtn = [ViewFactory makeNavigationBackWhite:self action:@selector(backClick)];
    self.navigationItem.leftBarButtonItem = backBtn;

    self.view.backgroundColor = [UIColor colorWithRed:32/255.0 green:32/255.0 blue:32/255.0 alpha:1.0f];
    _mainTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0,kScreenW , kScreenH-64) style:UITableViewStyleGrouped];
    _mainTableView.delegate = self;
    _mainTableView.dataSource = self;
    [self.view addSubview:_mainTableView];
    
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self request];
}
- (void)request
{
    NSString *urlStr = [NSString stringWithFormat:@"%@/userCenter/getUserById",APIYIGOU]
    ;
    DYBaseRequest *request = [DYBaseRequest new];
    request.requestUrl = urlStr;
    [request dyBgStartWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
        NSDictionary *allDic = [NSDictionary changeType:request.responseJSONObject];
        NSLog(@"111%@",allDic);
        _user = [[User alloc]initWithDic:allDic[@"data"]];
        [_mainTableView reloadData];
    } error:^(YTKBaseRequest *request) {
        
    } failure:^(YTKBaseRequest *request) {
        [ViewFactory showErrorStrMsg:NETWORK_ERR];
    }];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section==0) {
        return 4;
    }
    return 1;
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * identifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell)
    {
        cell =[[NSBundle mainBundle ]loadNibNamed:@"ZYTableViewCell" owner:nil options:nil][0];
    }
    NSArray *titleArr = [[NSArray alloc]initWithObjects:@"昵称",@"性别",@"个性签名",@"兴趣标签", @"收货地址",nil];
    UILabel *titleLable = [[UILabel alloc]init];
    titleLable.frame = CGRectMake(30*kScale, (100-44)*kScale/2, kScreenW, 44*kScale);
    UILabel *contentLable = [[UILabel alloc]init];
    if (indexPath.section==0)
    {
        titleLable.text = titleArr[indexPath.row];
        if (indexPath.row == 0)
        {
            if ([_user.name isEqualToString:@"<null>"])
            {
                contentLable.text = @"";
            }
            else
            {
                contentLable.text = _user.name;

            }
                     }
        else if (indexPath.row==1)
        {
            NSLog(@"999999%@",_user.sex);
            if ([_user.sex isEqual:[NSNull null]])
            {
                contentLable.text = @"";
            }
            else if([_user.sex isEqualToString:@"00"])
            {
                
                contentLable.text = @"男";
            }
            else if([_user.sex isEqualToString:@"01"])
            {
                contentLable.text = @"女";
            }

            
        }
        else if (indexPath.row==2)
        {
            if ([_user.personSign isEqual:[NSNull null]])
            {
                contentLable.text = @"";
            }
            else
            {
                contentLable.text = _user.personSign;
                
            }
            
        }
        else if (indexPath.row==3)
        {
            NSString *tagStrArr = nil;
            if (_user.loveSignArr.count>0)
            {
                for (int i=0;i<_user.loveSignArr.count ; i++)
                {
                    NSString *tagstr = _user.loveSignArr[i][@"tag_name"];
                    if (i==0)
                    {
                        tagStrArr = [NSString stringWithFormat:@"%@",tagstr];
                    }
                    else
                    {
                       tagStrArr = [NSString stringWithFormat:@"%@、%@",tagStrArr,tagstr]; 
                    }
                    contentLable.text = tagStrArr;
                }
            }
            
        }
    }
    else
    {
        titleLable.text = titleArr[indexPath.row+4];
    }
    CGSize titleSize = [titleLable sizeThatFits:CGSizeMake(MAXFLOAT, MAXFLOAT)];
    CGSize contentSize = [contentLable sizeThatFits:CGSizeMake(kScreenW-60*kScale, MAXFLOAT)];
    contentLable.frame = CGRectMake(60*kScale+titleSize.width, 0, kScreenW-90*kScale-60*kScale-titleSize.width, 100*kScale);
    contentLable.textAlignment = NSTextAlignmentRight;
    [cell addSubview:titleLable];
    
    [cell addSubview:contentLable];
    //右侧小箭头
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return  100*kScale;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section==0) {
        return 0.01;
    }
    return 20*kScale;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ChangeNameViewController *vc1 = [[ChangeNameViewController alloc]init];
    vc1.user = _user;
    ChangeSexViewController *vc2 = [[ChangeSexViewController alloc]init];
    vc2.user = _user;
    ChangePersonSignViewController *vc3 = [[ChangePersonSignViewController alloc]init];
    vc3.user = _user;
    ChangeLoveSignViewController *vc4 = [[ChangeLoveSignViewController alloc]init];
    AddressListViewController *vc5 = [[AddressListViewController alloc]init];
    NSMutableArray *vcArr = [NSMutableArray arrayWithObjects:vc1,vc2,vc3,vc4,vc5, nil];
    if (indexPath.section==0)
    {
        [self.navigationController pushViewController:vcArr[indexPath.row] animated:YES];
    }
    else
    {
         [self.navigationController pushViewController:vcArr[indexPath.row+4] animated:YES];
    }
    
}

- (void)backClick
{
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
